<?php
/**
 * Created by PhpStorm.
 * User: yellow
 * Date: 2019/7/12
 * Time: 14:55
 */

namespace app\admin\logic;


use app\admin\logic\code\ErrorCode;
use app\common\model\DeliveryBus;
use app\common\model\User;
use app\common\model\Order;
use app\common\model\OrderLog;
use app\common\model\OrderPlace;
use app\common\model\OrderPrice;
use app\vehicle\logic\TplLogic;
use app\vehicle\logic\tokenLogic;
class OrderLogic extends BaseLogic
{
    private $orderModel;

    public function __construct()
    {
        parent::__construct();
        $this->orderModel = new Order();
    }

    /**
     * 订单列表
     * @param $tempWhere
     * @param $nowPage
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function orderList($tempWhere, $nowPage){
        $role = session('admin_info');
        if($role['role_id']==2) {
            Order::where('id', 'gt', 0)->update(['is_shop_read' => 1]);
        }else{
            Order::where('id', 'gt', 0)->update(['is_read' => 1]);
        }
        $where = [];
        if ($tempWhere['order_no']){
            $where[] = ['order_no', 'like', "%".$tempWhere['order_no']."%"];
        }
        if($role['role_id']==2){
            $where[] = ['bus_id', 'eq',$role['id'] ];
        }else if($role['role_id']==3) {
            $where[] = ['shop_id', 'eq',$role['id'] ];
        }
        if (is_numeric($tempWhere['pay_status'])){
            $where[] = ['pay_status', '=', $tempWhere['pay_status']];
        }
        if (is_numeric($tempWhere['status'])){
            $where[] = ['status', '=', $tempWhere['status']];
        }
        if ($tempWhere['realname']){
            $where[] = ['realname', 'like', "%".$tempWhere['realname']."%"];
        }
        if ($tempWhere['mobile']){
            $where[] = ['mobile', 'like', "%".$tempWhere['mobile']."%"];
        }
        if ($tempWhere['driver']){
            $where[] = ['driver_id', 'eq',$tempWhere['driver']];
        }
        $count = $this->orderModel->where($where)->count();
        if ($count <= 0){
            return ajaxArr(ErrorCode::$LISTNULL, '列表为空');
        }
        $orderList = $this->orderModel->where($where)->order('update_time desc,id desc')->page($nowPage, self::$pageNum)->select();
        foreach($orderList as $k=>$v){
            $shop_name = db('admin')->where('id','eq',$v['shop_id'])->value('nick_name');
            $orderList[$k]['shop_name'] = $shop_name?$shop_name.'合作商':'自营';
        }
        return ajaxArr(ErrorCode::$SUCCESS, 'success', $orderList);
    }

    /**
     * 订单详情
     * @param $orderId
     */
    public function orderInfo($orderId){
        $where = [];
        if ($orderId){
            $where[] = ['id', '=', $orderId];
        }
        //订单基础信息
        $orderInfo = $this->orderModel->where($where)->find();

        $res['order_info'] = $orderInfo;
        
        if ($orderInfo){
            return ajaxArr(ErrorCode::$SUCCESS, 'success', $res);
        } else{
            return ajaxArr(ErrorCode::$INFONULL, '订单不存在');
        }
    }

    /**
     * 评论列表
     */
    private function commentList($orderId){
        if ($orderId){
            $where[] = ['order_id', '=', $orderId];
        }

    }

    /**
     * 订单金额详情
     * @param $orderId
     */
    public function orderPrice($orderId){
        if ($orderId){
            $where[] = ['order_id', '=', $orderId];
            $priceModel = new OrderPrice();
            $priceInfo = $priceModel->where($where)->find();
            if ($priceInfo){
                return ajaxArr(ErrorCode::$SUCCESS, 'success', $priceInfo);
            } else{
                return ajaxArr(ErrorCode::$INFONULL, '订单不存在');
            }
        } else{
            return ajaxArr(ErrorCode::$INFONULL, '订单不存在');
        }
    }
    public function handle1($data){
        $role = session('admin_info');
        $insert['shop_id'] = $role['id'];
        $insert['order_sn'] = date('YmdHis').rand(1000,9999);
        $insert['start_city'] = $data['start_city'];
        $insert['end_city'] = $data['end_city'];
        $insert['number'] = $data['number'];
        $insert['site'] = $data['site'];
        $insert['realname'] = $data['realname'];
        $insert['mobile'] = $data['mobile'];
        $insert['use_time'] = strtotime($data['use_time']);
        $insert['back_time'] && $data['back_time'] = strtotime($data['back_time']);
        $insert['back_site'] = $data['back_site'];
        $insert['update_time'] = $insert['create_time'] = time();
        
        $res = db('order')->insert($insert);
        if ($res){
            return ajaxArr(ErrorCode::$SUCCESS, '操作成功', $data);
        } else{
            return ajaxArr(ErrorCode::$OPERATIONERROR, '操作失败1'.$res);
        }
    }
    /**
     * 处理订单逻辑
     * @param $data
     */
    public function handle($data){
        $role = session('admin_info');
        $act = $data['act'];
        $res = false;
        switch ($act){
            case 'price':
                //修改订单金额
                $priceModel = new OrderPrice();
                $res = $priceModel->allowField(true)->save($_POST,['order_id' => $data['order_id']]);
                unset($data['order_id']);
                break;
            case 'remark':
                //修改订单备注
                $info = Order::get($data['order_id']);
                $info->remark = $data['remark'];
                $res = $info->save();
            case 'driver':
                $up['order_price'] = $data['order_price'];
                $up['driver_id']= $data['driver_id'];
                $up['update_time'] = time();
                $res = Order::where('id','eq',$data['order_id'])->update($up);

                $info = Order::get($data['order_id']);
                $user = User::where('id','eq',$info['user_id'])->find();
                if($user['open_id'] && $user['form_id']){
                    TplLogic::confirmOrder($user['open_id'],$user['form_id'],$info['order_sn'],$up['order_price'],$data['order_id']);
                }

                $res = json_encode($data);
                break;
            case 'order_status':
                $up['status'] = $data['status'];
                $data['status']==1 && $up['bus_id'] = $data['shop_id'];
                $res = Order::where('id','eq',$data['order_id'])->update($up);
                $insert['order_id'] = $data['order_id'];
                $insert['status'] = $data['status'];
                $insert['user_id'] = $role['id'];
                $insert['create_time'] = time();
                OrderLog::insert($insert);
                break;
            default:
                break;
        }
        if ($res){
            return ajaxArr(ErrorCode::$SUCCESS, '操作成功', $data);
        } else{
            return ajaxArr(ErrorCode::$OPERATIONERROR, '操作失败');
        }
    }

}